/**
 * 校验和解析类
 */
package com.flute.framework.configuration;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import com.flute.framework.business.TimerChecksum;
import com.flute.framework.data.impl.CheckSumHandler;
import com.flute.framework.data.impl.RawSumCheckParser;
import com.flute.framework.exception.ConfigurationParseException;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

/**
 * @author Jazz
 * 
 */
public class ChecksumXMLConverter {
	public static TimerChecksum getTimerChecksumInfo(InputStream inputStream)
			throws ConfigurationParseException {
		try {
			return (TimerChecksum) getXStream().fromXML(inputStream);
		} catch (Exception e) {
			throw new ConfigurationParseException(e);
		}
	}

	private static XStream getXStream() {
		XStream xs = new XStream(new DomDriver());
		xs.alias("timer-checksum", TimerChecksum.class);
		xs.aliasAttribute(TimerChecksum.class, "startHour", "start-hour");
		xs.aliasAttribute(TimerChecksum.class, "startMinute", "start-minute");
		xs.aliasField("checksum-handler", TimerChecksum.class, "handler");

		xs.alias("checksum-handler", CheckSumHandler.class);
		xs.aliasAttribute(CheckSumHandler.class, "checkSumPath", "path");
		xs.aliasAttribute(CheckSumHandler.class, "backupPath", "backup");
		xs.aliasAttribute(CheckSumHandler.class, "updateThreshold",
				"update-threshold");
		xs.aliasAttribute(CheckSumHandler.class, "name", "name");
		xs.aliasAttribute(CheckSumHandler.class, "failedPath",
				"failed-folder-path");
		xs.aliasField("checksum-parser", CheckSumHandler.class, "parser");

		xs.alias("checksum-parser", RawSumCheckParser.class);

		return xs;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {
			XStream xs = new XStream(new DomDriver());
			try {
				System.out
						.println(xs
								.toXML(getTimerChecksumInfo(new FileInputStream(
										new File(
												"D:/workspace/lily/src/framework/config/preparer/checksum/checksum.xml")))));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ConfigurationParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	}

}
